﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DacSanMienTayOBJ;
public partial class UserControl_ManagerCategory : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void Addcategory()
    {
        try
        {
            string categoryName = Request["txtCategoryName"];
            string metakey = Request["txtKeyWord"];

            string fileImages = "";
            if (fileLogo.FileName != "")
            {
                string image = Server.MapPath("~/ImageUpload/" + fileLogo.FileName);
                fileLogo.PostedFile.SaveAs(image);
                fileImages = "ImageUpload/" + fileLogo.FileName;
            }
            int section = int.Parse(Request["hdTypeCate"]);
            int parentCateID = 0;
            try
            {
                if (section == 0)
                {
                    parentCateID = int.Parse(Request["cbParentCate"]);
                }
                else if (section == 2)
                {
                    parentCateID = int.Parse(Request["cbMiddleCate"]);
                }
            }
            catch
            {

            }
            Category cate = new Category();
            cate.CategoryImage = fileImages;
            cate.CategoryName = categoryName;
            cate.IsVisible = 1;
            cate.MetaKeyword = metakey;
            cate.Priority = 1;
            cate.ParentCategoryID = parentCateID;
            cate.IsSection = section;

            bool rs = CategoryBusiness.InsertCategory(cate, section, ConnectionType.AdminConnection);
            if (rs)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã thêm category')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Không thêm được category. Vui lòng liên hệ admin')", true);
            }
        }
        catch(Exception ex)
        {
            Utilitys.WriteLog(ex.Message);
        }
    }
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        Addcategory();
    }

    private void UpdateCate()
    {
        try
        {
            string categoryName = Request["txtCategoryName"];
            string metakey = Request["txtKeyWord"];

            string fileImages = "";
            if (fileLogo.FileName != "")
            {
                string image = Server.MapPath("~/ImageUpload/" + fileLogo.FileName);
                fileLogo.PostedFile.SaveAs(image);
                fileImages = "ImageUpload/" + fileLogo.FileName;
            }    
            int section = int.Parse(Request["hdTypeCate"]);
            int parentCateID = 0;
            try
            {
                if (section == 0)
                {
                    parentCateID = int.Parse(Request["cbParentCate"]);
                  
                }
                else if (section == 2)
                {
                    parentCateID = int.Parse(Request["cbMiddleCate"]);
                   
                }
            }
            catch
            {

            }
          
            int cateID =int.Parse(Request["hdCateID"]);
            Category cate = new Category();
            cate.CategoryImage = fileImages;
            cate.CategoryName = categoryName;;
            cate.MetaKeyword = metakey;
            cate.ParentCategoryID = parentCateID;
            cate.IsSection = section;
            bool rs = CategoryBusiness.UpdateCategory(cateID, cate, section, ConnectionType.AdminConnection);
            if (rs)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã cập nhật category')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Không cập nhật được category. Vui lòng liên hệ admin')", true);
            }
        }
        catch (Exception ex)
        {
            Utilitys.WriteLog(ex.Message);
        }
    }
    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        UpdateCate();
    }
}